package bb.lanxing.lib.devices.bici.cmd;

import com.umeng.analytics.pro.cc;
import bb.lanxing.lib.devices.bici.BLECommand;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Map;

public class UserConnectCMD extends BLECommand implements Serializable {
    public static final int BIKE_ID_LEN = 16;
    public static final byte BIND_FAILED = 0;
    public static final byte BIND_SUCCESS = 1;
    public static final byte COMMAND_ID = 3;
    public static final byte CONNECT_FAILED = 0;
    public static final byte CONNECT_SUCCESS = 1;
    public static final byte KEY_AUTH_CODE = 2;
    public static final byte KEY_BIKE_ID = 4;
    public static final byte KEY_BIND_RESULT = 3;
    public static final byte KEY_CONNECT_RESULT = 5;
    public static final byte KEY_UNBIND = 6;
    public static final byte KEY_USER_ID = 1;
    private String authCode;
    private byte[] bikeId;
    private byte bindResult;
    private byte connectResult;
    private int userId;

    public UserConnectCMD() {
        this.commandId = (byte) 3;
    }

    public UserConnectCMD(byte[] bArr) {
        super(bArr);
    }

    @Override
    public void parseBody(byte[] bArr) {
        if (bArr.length < 3) {
            return;
        }
        ByteBuffer wrap = ByteBuffer.wrap(bArr);
        byte b = wrap.get();
        int i = wrap.getShort();
        byte[] bArr2 = new byte[(bArr.length - i) - 3];
        if (b == 1) {
            this.userId = wrap.getInt();
        } else if (b == 2) {
            byte[] bArr3 = new byte[i];
            wrap.get(bArr3);
            this.authCode = new String(bArr3);
        } else if (b == 3) {
            this.bindResult = wrap.get();
        } else if (b == 4) {
            byte[] bArr4 = new byte[i];
            this.bikeId = bArr4;
            wrap.get(bArr4);
        } else if (b == 5) {
            this.connectResult = wrap.get();
        }
        wrap.get(bArr2);
        parseBody(bArr2);
    }

    public String toString() {
        return "UserConnectCMD: userId = " + this.userId + "\n authCode = " + this.authCode + "\n bindResult = " + ((int) this.bindResult) + "\n bikeId = " + new String(this.bikeId) + "\n connectResult = " + ((int) this.connectResult);
    }

    @Override
    protected byte[] encodeCMDBody(Map<Byte, Object> map) {
        int i;
        ByteBuffer allocate = ByteBuffer.allocate(500);
        allocate.put((byte) 3);
        allocate.put((byte) 16); //cc.n
        if (map.get((byte) 1) != null) {
            allocate.put((byte) 1);
            allocate.putShort((short) 4);
            allocate.putInt((Integer) map.get((byte) 1));
            i = 9;
        } else {
            i = 2;
        }
        if (map.get((byte) 3) != null) {
            allocate.put((byte) 3);
            allocate.putShort((short) 1);
            allocate.put((Byte) map.get((byte) 3));
            i += 4;
        }
        if (map.get((byte) 2) != null) {
            allocate.put((byte) 2);
            String str = (String) map.get((byte) 2);
            allocate.putShort((short) str.getBytes().length);
            allocate.put(str.getBytes());
            i = i + 3 + str.getBytes().length;
        }
        if (map.get((byte) 4) != null) {
            allocate.put((byte) 4);
            allocate.putShort((short) 4);
            byte[] bArr = (byte[]) map.get((byte) 4);
            allocate.put(bArr);
            i = i + 3 + bArr.length;
        }
        if (map.get((byte) 5) != null) {
            allocate.put((byte) 5);
            allocate.putShort((short) 1);
            allocate.put((Byte) map.get((byte) 5));
            i += 4;
        }
        if (map.get((byte) 6) != null) {
            allocate.put((byte) 6);
            allocate.putShort((short) 1);
            allocate.put((Byte) map.get((byte) 6));
            i += 4;
        }
        byte[] bArr2 = new byte[i];
        allocate.flip();
        allocate.get(bArr2);
        return bArr2;
    }

    public int getUserId() {
        return this.userId;
    }

    public String getAuthCode() {
        return this.authCode;
    }

    public byte getBindResult() {
        return this.bindResult;
    }

    public String getBikeId() {
        byte[] bArr = this.bikeId;
        if (bArr == null) {
            return null;
        }
        return new String(bArr);
    }

    public byte getConnectResult() {
        return this.connectResult;
    }
}
